package com.android.picotts.util;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.text.TextUtils;

public class IntentUtils {
    
    /** 打开一个应用*/
    public static Intent openApplacation(Context context, String appPackageName) {
        PackageManager pm = context.getPackageManager();
        try {
            Intent appStartIntent = pm.getLaunchIntentForPackage(appPackageName);
            return appStartIntent;
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 发送邮件
     */
    public static Intent sendEmail(String to, String subject, String text) {
        return sendEmail(new String[]{to}, subject, text);
    }

    /**
     * 群发邮件
     */
    public static Intent sendEmail(String[] to, String subject, String text) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, text);
        return intent;
    }

    /**
     *  调用发送短信APP
     */
    public static Intent sendSms(String to, String message) {
        Uri smsUri = Uri.parse("tel:" + to);
        Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
        intent.putExtra("address", to);
        intent.putExtra("sms_body", message);
        intent.setType("vnd.android-dir/mms-sms");
        return intent;
    }

    /**
     * 打开指定位置用地图APP
     */
    public static Intent showLocation(float latitude, float longitude, Integer zoomLevel) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        String data = String.format("geo:%s,%s", latitude, longitude);
        if (zoomLevel != null) {
            data = String.format("%s?z=%s", data, zoomLevel);
        }
        intent.setData(Uri.parse(data));
        return intent;
    }

    /**
     * 打开一个WEB地址
     */
    public static Intent openLink(String url) {
        if (!TextUtils.isEmpty(url) && !url.contains("://")) {
            url = "http://" + url;
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        return intent;
    }

    /** 打开文件*/
    public static Intent openFile(String fileStr, Context c) {
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);

        File file = new File(fileStr);
        Uri data = Uri.fromFile(file);
        String type = MimeTypes.getInstance(c).getMimeType(file.getName());      
        intent.setDataAndType(data, type);
        
        return intent;
    }

    /** 打开拨打电话，等待拨打*/
    public static Intent callPhone(String phoneNumber) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        return intent;
    }
    
    /** 获取通讯录的一个联系人*/
    public static Intent pickContact() {
        return pickContact(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    }

    /**
     * Pick contact from phone book
     *
     * @param scope You can restrict selection by passing required content type. Examples:
     *              <p/>
     *              <code><pre>
     *              // Select only from users with emails
     *              IntentUtils.pickContact(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);
     *              <p/>
     *              // Select only from users with phone numbers on pre Eclair devices
     *              IntentUtils.pickContact(Contacts.Phones.CONTENT_TYPE);
     *              <p/>
     *              // Select only from users with phone numbers on devices with Eclair and higher
     *              IntentUtils.pickContact(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
     *              </pre></code>
     */
    public static Intent pickContact(String scope) {
        Intent intent;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ECLAIR) {
            intent = new Intent(Intent.ACTION_PICK, Contacts.People.CONTENT_URI);
        } else {
            intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://com.android.contacts/contacts"));
        }
        if (!TextUtils.isEmpty(scope)) {
            intent.setType(scope);
        }
        return intent;
    }

    /**
     * 立即拨通电话
     */
    public static Intent dialPhone(String phoneNumber) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        return intent;
    }
    
    /** 回到桌面*/
    public static Intent backHome(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        return  intent;
    }
}